package com.bsh.api;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bsh.api.Exception.ApiException;
import com.bsh.service.ApiUserService;
import com.bsh.utils.ApiMessage;
import com.bsh.utils.IpUtil;

/**
 * 验证相关API
 * 
 * @author gsh
 * @since 0.0.1
 * 
 */
@Controller
public class ApiAuthController {
	private static Logger log = LoggerFactory
			.getLogger(ApiAuthController.class);
	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;
	@Autowired
	HttpSession session;

	

	@Autowired
	ApiUserService apiUserService;


	/**
	 * 用户登录
	 * @param paramStr
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/api/v1/login", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage login(@RequestParam String paramStr) throws Exception {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			dto.put("picUrl", ApiUtils.getPicUrl(request));
			dto.put("ip", IpUtil.getIpAddr(request));
			msg = apiUserService.login(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}
	}

	/**
	 * 实名认证
	 * @param paramStr
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/api/v1/authIdentify", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage authNameIdentify(@RequestParam String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			dto.put("ip", IpUtil.getIpAddr(request));
			msg = apiUserService.authNameIdentify(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}

	}
	/**
	 * 照片认证
	 * @param paramStr
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/api/v1/authPicIdentify", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage authPicIdentify(@RequestParam String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			dto.put("serv_ip", ApiUtils.getPicUrl(request));
			msg = apiUserService.authPicIdentify(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}

	}
	

}
